package com.example.demo.config;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/myWs/{userId}")
@Component
public class ZuWangWebSocket {

    /**
     * 当前连接数
     */
    private static int onlineCount = 0;

    /**
     * concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象
     */
    private static CopyOnWriteArraySet<ZuWangWebSocket> webSocketSet = new CopyOnWriteArraySet<>();

    /**
     * 与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    private Session session;

    /**
     * 接收的用户id
     */
    private String userId;

    /**
     * 连接成功
     *
     * @param session
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        this.session = session;
        webSocketSet.add(this);
        addOnlineCount();        //在线数加1
        System.out.println("连接成功");
    }

    /**
     * 连接关闭
     *
     * @param session
     */
    @OnClose
    public void onClose(Session session) {
        System.out.println("连接关闭");
        CopyOnWriteArraySet<ZuWangWebSocket> webSocketSet1 = webSocketSet;
        webSocketSet1.clear();
    }

    /**
     * 接收到消息
     *
     * @param text
     */
    @OnMessage
    public String onMsg(String text) throws IOException {
        System.out.println("收到消息 ： "+text);
        return "收到了，给你返回这个";
    }


    /**
     * 收到的消息是
     * {"device":"GTCZ1","target":"YSRP","address":{"ip":"192.168.8.233","mask":"255.255.255.0","gateway":"192.168.8.1","dns":"8.8.8.8"},"transtype":"1001"}
     */
    /*
      returnd的消息是 {"device":"YSRP","sn":"G1116000069012345670120210429001","transtype":"1000"}
     */
    /**
     * 获取在线人数
     * @return 在线人数
     */
    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    /**
     * 在线人数加一
     */
    public static synchronized void addOnlineCount() {
        ZuWangWebSocket.onlineCount++;
    }

    /**
     * 在线人数减一
     */
    public static synchronized void subOnlineCount() {
        ZuWangWebSocket.onlineCount--;
    }

    /**
     * 发送消息
     * @param message 消息
     * @throws IOException 异常
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    public void sendInfo(String message) throws Exception{

        for (ZuWangWebSocket wangWebSocket : webSocketSet) {

//            wangWebSocket.sendMessage(message);
            try {
                //这里可以设定只推送给这个sid的，为null则全部推送
                if (userId == null) {
                    wangWebSocket.sendMessage(message);
                } else if (wangWebSocket.userId.equals(userId)) {
                    wangWebSocket.sendMessage(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

